//
//  OSMineView.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/29.
//

#import "OSMineView.h"
#import "OSMineCell.h"
#import "OSSettingVC.h"
#import "OSAboutUsVC.h"
#import "OSMyOrderVC.h"
static  NSString *const kCellId = @"OSMineCell";
@interface OSMineView()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation OSMineView
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.tbBgView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    _tipLbl1.text = kLocalString(@"正规金融机构");
    _tipLbl2.text = kLocalString(@"急速审核，闪电放款！");
    NSArray *titles = @[kLocalString(@"我的借款"),kLocalString(@"帮助中心"),kLocalString(@"常见问题"),kLocalString(@"联系客服"),kLocalString(@"关于我们"),kLocalString(@"设置")];
    NSArray *images = @[@"icon_qianbao",@"icon_dengpao",@"icon_wenti",@"icon_kefu",@"icon_guanyu",@"icon_setting"];
    
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setObject:obj forKey:@"title"];
        [dict setObject:images[idx] forKey:@"image"];
        [self.dataArray addObject:dict];
    }];
    
    _topView.layer.shadowColor = [UIColor colorWithRed:14/255.0 green:38/255.0 blue:115/255.0 alpha:0.05].CGColor;
    _topView.layer.shadowOffset = CGSizeMake(0,6);
    _topView.layer.shadowOpacity = 1;
    _topView.layer.shadowRadius = 11;
    _topView.layer.cornerRadius = 10;
    
    _nameLbl.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (kIsTourst) {
            [OSTool pushToLogVc];
        }
    }];
    [_nameLbl addGestureRecognizer:tap];
}


#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OSMineCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    NSDictionary *dict = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLbl.text = dict[@"title"];
    cell.icon.image = [UIImage imageNamed:dict[@"image"]];
    
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        headview.contentView.backgroundColor = kClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = kClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = _dataArray[indexPath.row];
    NSString *title = dict[@"title"];
    OSRootViewController *curVc = (OSRootViewController *)[OSTool getCurrentUIVC];
    if ([title isEqualToString:kLocalString(@"设置")]) {
        OSSettingVC *vc = [OSSettingVC new];
        vc.title = title;
        [curVc.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"联系客服")]){
        NSMutableString *telStr = [[NSMutableString alloc] initWithFormat:@"tel:18888888888"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telStr]];
    }else if ([title isEqualToString:kLocalString(@"关于我们")]){
        OSAboutUsVC *vc = [OSAboutUsVC new];
        vc.title = title;
        [curVc.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"我的借款")]){
        OSMyOrderVC *vc = [OSMyOrderVC new];
        vc.title = title;
        [curVc.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"常见问题")]){
        LTBaseWebViewController *vc = [LTBaseWebViewController new];
        vc.title = title;
        vc.homeURL = @"https://www.baidu.com";
        [curVc.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"帮助中心")]){
        LTBaseWebViewController *vc = [LTBaseWebViewController new];
        vc.title = title;
        vc.homeURL = @"https://www.baidu.com";
        [curVc.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 85;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = kClearColor;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


@end
